#include "e2d/core/impl/components.h"
#include "e2d/core/Entity.h"

std::vector<std::string> e2d::impl::Components::m_Labels= {
	e2d::Prefeb::_Label_(),
	e2d::MonoScripts::_Label_(),
   };

bool e2d::impl::Components::AddComponent(e2d::Entity* entity, const e2d::ComponentType& type)
{
	bool flag{ 0 };

    flag |= entity->AddComponentFromType<e2d::Prefeb>(type);
    flag |= entity->AddComponentFromType<e2d::MonoScripts>(type);
	return flag;
}

bool e2d::impl::Components::CopyComponents(const e2d::Entity* source, e2d::Entity* target)
{
	bool flag{ 0 };

	flag |= target->CopyComponent<e2d::Prefeb>(source);
	flag |= target->CopyComponent<e2d::MonoScripts>(source);

	return flag;
}

bool e2d::Components::AddComponent(e2d::Entity* entity, const ComponentType& type)
{
	bool flag{ 0 };

#ifdef E2D_BUILD_TRANS
	flag |= e2d::trans::Components::AddComponent(entity, type);
#endif
#ifdef E2D_BUILD_RENDER
	flag |= e2d::render::Components::AddComponent(entity, type);
#endif
#ifdef E2D_BUILD_AUDIO
	flag |= e2d::audio::Components::AddComponent(entity, type);
#endif
#ifdef E2D_BUILD_PHYSICS
	flag |= e2d::physics::Components::AddComponent(entity, type);
#endif
#ifdef E2D_BUILD_FUI
	flag |= e2d::fui::Components::AddComponent(entity, type);
#endif

	flag |= e2d::impl::Components::AddComponent(entity, type);


	return flag;
}


namespace
{
	template<typename T>
	bool CopyComponent(const e2d::Entity* source, e2d::Entity* target)
	{
		bool flag = source->HasComponent<T>();
		if (flag)
		{
			target->EmplaceOrReplace<T>(source->GetConst<T>());
		}
		return flag;
	}
}


void e2d::Components::CopyEntity(const e2d::Entity* source, e2d::Entity* target, bool copyPositionData)
{
	target->m_Name = source->m_Name;
	CopyEntityComponents(source, target, copyPositionData);

	target->m_Children.clear();
	for (auto child : source->m_Children)
	{
		auto childToken = target->NewChild();
		CopyEntity( child.get(), childToken.get(), true);
	}

};


bool e2d::Components::CopyEntityComponents(const e2d::Entity* source, e2d::Entity* target, bool copyPositionData)
{
	bool flag{ 0 };

#ifdef E2D_BUILD_TRANS
	flag |= e2d::trans::Components::CopyComponents(source, target, copyPositionData);
#endif
#ifdef E2D_BUILD_RENDER
	flag |= e2d::render::Components::CopyComponents(source, target);
#endif
#ifdef E2D_BUILD_AUDIO
	flag |= e2d::audio::Components::CopyComponents(source, target);
#endif
#ifdef E2D_BUILD_PHYSICS
	flag |= e2d::physics::Components::CopyComponents(source, target);
#endif
#ifdef E2D_BUILD_FUI
	flag |= e2d::fui::Components::CopyComponents(source, target);
#endif

	flag |= e2d::impl::Components::CopyComponents(source, target);

	return flag;
};

